/*
 *  treehash.h
 *
 * Set of functions for creating/manipulating/comparing SHA1 hash
 * tree for use with files, mostly for P2P applications.  
 * Built on mini_db for storage purposes.  These function 
 * are by no means standardized, and provides interpolarity only among itself.
 *
 *
 * To obtain the root hash just run treehash_sample, and treehash_promote until
 * it returns non-zero.  To obtain the root hash just mdb_get name "hash-0-0".
 *
 * Because mini_db is the storgage media, all items are stored by name and value.
 * This includes hashes, and even options.
 * Hash are named "hash-#depth_off-#item_off" and store SHA1 hashes. 
 * The only setting used is in treehash_sample, which is "samples", its value is a
 * number, however it is still a string.
 * 
 * Options:
 *    "filename" - Exact path passed to treehash_sample
 *    "filesize" - off_t of filename's filesize
 *    "sample"   -  Size of any given sample.
 *    "curdepth" - Includes the value of the last computed depth (NOTE: is offset, root is 0)
 *    "method"   - Hash scheme used (only SHA1 supported)
 *    "bad"      - Placed in questionable hash during treehash_cmp
 *    "hash-0-0" - Root hash
 *    "hash-1-0" - Left hash used to generate root
 *    "hash-1-1" - Right hash used to generate root
 *    "hash-2-0" - Left hash used to generate "hash-1-0"
 *   .........And so.......
 *
 *
 * Imagine a tree, depths are formed by sample, or the depth ABOVE them!
 *
 *   Depth:  |         Tree:
 * ----------+------------------------------
 *     0     |                  0
 *     1     |             0_/     \_1
 *     2     |         0_/   \_1 2_/  \_3
 *     3     |       0/\1  2/\3  4/\5  6/\7
 *
 * If you want to ESIMATE how many items are at any given depth, 
 * just use this formula:
 *            items = 2^depth
 *		
 *  Check out http://open-content.net/specs/draft-jchapweske-thex-02.html
 *  for a SIMILAR application.  Please note it is not the same, and these
 *  function will produce different hashes!
 *
 * Functions:
 * 	treehash_promote:  
 * 		Args:     st_hashtree data and option to delete previous hash
 *			   depth.
 * 		Returns:  zero when not root, when root has been calculated
 *			   non-zero is returned.
 *     treehash_sample:  
 * 		Args:     st_hashtree data and filename
 * 		Returns:  Calculated depth of tree.
 *
 * Also:
 *     A st_treehash may be
 *     manipulated by mini_db
 *     mdb functions.
 *
 * Copyright (c) 2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef ___TREEHASHES_H
#define ___TREEHASHES_H

#include "string.h"
#include "mini_db.h"
#include "math.h"
#include "file.h"

#include "md4.h"
#include "md5.h"
#include "sha1.h"

#define DEFAULT_SAMPLE 1024
#define MAX_SAMPLE 16384

#define MD4_DIGEST_LEN  16
#define MD5_DIGEST_LEN  16
#define SHA1_DIGEST_LEN 20

#define MD4_DIGEST  1
#define MD5_DIGEST  2
#define SHA1_DIGEST 3

/*Yet another set of functions which uses mini_db*/
#define st_treehash         st_mdb
#define treehash_init       mdb_init
#define treehash_set        mdb_set
#define treehash_set_int    mdb_set_int
#define treehash_set_hash   mdb_set_base32
#define treehash_set_off_t  mdb_set_off_t
#define treehash_copy       mdb_copy
#define treehash_clone      mdb_clone
#define treehash_print      mdb_print
#define treehash_get        mdb_get
#define treehash_get_int    mdb_get_int
#define treehash_get_off_t  mdb_get_off_t
#define treehash_free       mdb_free
#define treehash_save       mdb_save
#define treehash_load       mdb_load
#define treehash_reset      mdb_reset

int badzero(const char * filename, off_t start, off_t end);

int treehash_badrun(struct st_treehash * hash_data,
		    int(*badfunc)(const char *, off_t, off_t)  );

char * treehash_gethash(struct st_treehash * hash_data, int depth, int item);
int treehash_sethash(struct st_treehash * hash_data, int depth, int item, char * hash);
int treehash_cmp(struct st_treehash * hash, struct st_treehash * questionable);
int treehash_promote(struct st_treehash * hash_data, int delete_previous);
int treehash_sample(struct st_treehash * hash_data, char * filename, int digest_type);



#endif
